<template>
  <div>
    <!-- 面包屑导航 -->
    <Breadcrumb />
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            v-model="queryInfo.query"
            clearable
            @clear="getGoodsList()"
            placeholder="请输入内容"
            @keyup.enter.native="getGoodsList()"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              style="background: #66b1ff; color: #fff"
              @click="getGoodsList()"
              >搜索</el-button
            >
          </el-input>
        </el-col>
        <el-col :span="6">
          <el-button type="primary" @click="goAddPage">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <el-table :data="goodsList" border stripe>
        <el-table-column align="center" type="index" label="序号" width="80">
        </el-table-column>
        <el-table-column
          align="center"
          prop="goods_name"
          label="商品名称"
          width="600"
        >
        </el-table-column>
        <el-table-column
          align="center"
          prop="goods_price"
          label="商品价格（元）"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="goods_weight"
          label="商品重量"
        ></el-table-column>
        <el-table-column align="center" label="创建时间">
          <template slot-scope="{ row }">
            {{ row.add_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="{ row }">
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-edit"
              @click="showEdit(row)"
            ></el-button>
            <el-button
              @click="deleteGoods(row.goods_id)"
              type="danger"
              size="mini"
              icon="el-icon-delete"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页器 -->
      <el-pagination
        background
        style="text-align: center; margin-top: 20px"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "List",
  data() {
    return {
      //查询参数对象
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 5,
      },
      //商品列表
      goodsList: [],
      //商品总数
      total: 0,
    };
  },
  created() {
    this.getGoodsList();
  },
  methods: {
    async getGoodsList() {
      const res = await this.$http.getGoodsList(this.queryInfo);
      if (res.meta.status !== 200) {
        this.$message.error("获取商品参数列表失败");
      }
      this.goodsList = res.data.goods;
      this.total = res.data.total;
    },
    //每页数量变化
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize;
      this.getGoodsList();
    },
    //当前页变化
    handleCurrentChange(page) {
      this.queryInfo.pagenum = page;
      this.getGoodsList();
    },
    //删除商品
    deleteGoods(id) {
      this.$confirm("此操作将永久删除该商品, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const res = await this.$http.deleteGoods(id);
          if (res.meta.status === !200) {
            this.$message.error("删除商品失败");
          }
          this.$message.success("删除商品成功");
          this.getGoodsList();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //点击添加商品按钮
    goAddPage() {
      //去添加商品页面
      this.$router.push("/hgoods/goods/add");
    },
    //点击编辑商品
    showEdit(row) {
      const {goods_id,goods_name,goods_cat,goods_price,goods_number,goods_weight}=row
      let goods={goods_name,goods_cat,goods_price,goods_number,goods_weight}
    },
  },
};
</script>

<style lang="less" scoped>
</style>
